package com.tinem.platform.service.user.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baidu.fsg.uid.UidGenerator;
import com.tinem.platform.module.pojo.vo.user.AddReq;
import com.tinem.platform.module.starter.jpa.entity.rbac.RbacUser;
import com.tinem.platform.module.starter.jpa.repository.rbac.RbacUserRepository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className UserApi
 * @description TODO
 * @site
 * @company
 * @create 2020-09-03 15:29
 */
@RestController
@RequestMapping("/user")
public class UserApi {
    @Resource
    UidGenerator uidGenerator;
    @Resource
    RbacUserRepository rbacUserRepository;

    @PostMapping("/add.api")
    public RbacUser add(@Valid @RequestBody AddReq user) {
        RbacUser rbacUser = new RbacUser();
        BeanUtil.copyProperties(user,rbacUser);
        if(StrUtil.isEmpty(rbacUser.getAccount())){
            rbacUser.setAccount(rbacUser.getId());
        }
        return rbacUserRepository.saveAndFlush(rbacUser);
    }
    @PostMapping("/addByEmail.api")
    public RbacUser addByEmail(@Valid @RequestBody AddReq user) {

        RbacUser rbacUser = rbacUserRepository.findFirstByEmail(user.getEmail());
        if(rbacUser == null){
            rbacUser = new RbacUser();
        }else{
            return rbacUser;
        }
        BeanUtil.copyProperties(user,rbacUser);
        if(StrUtil.isEmpty(rbacUser.getAccount())){
            rbacUser.setAccount(rbacUser.getId());
        }
        return rbacUserRepository.saveAndFlush(rbacUser);
    }
}
